package com.itheima.zuoye.zy2;

import java.util.Arrays;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[5];
        Scanner scanner = new Scanner(System.in);

        // 输入学生信息
        for (int i = 0; i < students.length; i++) {
            System.out.println("请输入第" + (i + 1) + "个学生的身高：");
            double height = scanner.nextDouble();
            System.out.println("请输入第" + (i + 1) + "个学生的姓名：");
            String name = scanner.next();
            students[i] = new Student(name, height);
        }

        System.out.println("原始数据：");
        System.out.println(Arrays.toString(students));

        // 冒泡排序：按身高升序
        for (int i = 0; i < students.length - 1; i++) {
            for (int j = 0; j < students.length - 1 - i; j++) {
                if (students[j].getHeight() > students[j + 1].getHeight()) {
                    Student temp = students[j];
                    students[j] = students[j + 1];
                    students[j + 1] = temp;
                }
            }
        }

        System.out.println("冒泡排序后：");
        System.out.println(Arrays.toString(students));

        // 选择排序：按身高升序
        for (int i = 0; i < students.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < students.length; j++) {
                if (students[j].getHeight() < students[minIndex].getHeight()) {
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                Student temp = students[i];
                students[i] = students[minIndex];
                students[minIndex] = temp;
            }
        }

        System.out.println("选择排序后：");
        System.out.println(Arrays.toString(students));
    }
}